package com.company.regist.dao;


import com.company.regist.pojo.Register;
import com.company.regist.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class RegisterDAOImpl implements IRegisterDAO{


	@Override
	public int insert(Register r) throws SQLException {
		String sql="insert into regisiter values(?,?,?,?,?,?,?,?,?,?,?)";

		String sql2="insert into user values(null,?,?)";
		Connection conn= JDBCUtils.getConnection();
		PreparedStatement stmt=conn.prepareStatement(sql);
		PreparedStatement stmt2=conn.prepareStatement(sql2);
		stmt.setString(1,r.getUsername());
		stmt.setString(2,r.getPassword());
		stmt.setString(3,r.getSex());
		stmt.setString(4,r.getMarry());
		stmt.setString(5,r.getBirthday());
		stmt.setString(6,r.getTel());
		stmt.setString(7,r.getQq());
		stmt.setInt(8,r.getAge());
		stmt.setString(9,r.getEdu());
		stmt.setString(10,r.getLikes());
		stmt.setString(11,r.getIntro());


		stmt2.setString(1,r.getUsername());
		stmt2.setString(2,r.getPassword());

		int result;
		int result2 = 0;
		try{
			result=stmt.executeUpdate();
			result2=stmt2.executeUpdate();
		}
		catch (SQLException throwables) {
			throwables.printStackTrace();
			result=-1;
		}
		System.out.println("影响行数"+result);
		System.out.println("影响行数"+result2);
		JDBCUtils.destory(conn,stmt,null);
		return result;
	}

	@Override
	public int updateByUserName(String username,String newpassword) throws SQLException {

		String sql = "update user set password=? where username = ?";

		// 1. 得到连接
		Connection conn = JDBCUtils.getConnection();
		// 2. 创建查询对象
		PreparedStatement stmt = conn.prepareStatement(sql);
		stmt.setString(1, newpassword);
		stmt.setString(2,username);
		// 3. 执行
		int result=-1;
		try {
			result= stmt.executeUpdate();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		System.out.println("影响行数:"+result);
		// 4. 释放资源
		JDBCUtils.destory(conn, stmt, null);
		return result;
	}
}
